package com.risk.management.service.impl;

import com.risk.management.config.FileUploadConfig;
import com.risk.management.entity.RiskAttachment;
import com.risk.management.mapper.RiskAttachmentMapper;
import com.risk.management.service.RiskAttachmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.apache.commons.io.FilenameUtils;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
public class RiskAttachmentServiceImpl implements RiskAttachmentService {

    @Autowired
    private FileUploadConfig fileUploadConfig;

    @Autowired
    private RiskAttachmentMapper riskAttachmentMapper;

    @Override
    @Transactional
    public RiskAttachment uploadAttachment(Long riskId, MultipartFile file, String uploadBy) {
        try {
            String originalFilename = file.getOriginalFilename();
            String extension = FilenameUtils.getExtension(originalFilename);
            String newFilename = UUID.randomUUID().toString() + "." + extension;
            
            // 确保上传目录存在
            File uploadDir = new File(fileUploadConfig.getUploadPath());
            if (!uploadDir.exists()) {
                uploadDir.mkdirs();
            }
            
            // 保存文件
            File destFile = new File(uploadDir, newFilename);
            file.transferTo(destFile);
            
            // 创建附件记录
            RiskAttachment attachment = new RiskAttachment();
            attachment.setRiskId(riskId);
            attachment.setFileName(newFilename);
            attachment.setOriginalFileName(originalFilename);
            attachment.setFileType(file.getContentType());
            attachment.setFilePath(destFile.getAbsolutePath());
            attachment.setFileSize(file.getSize());
            attachment.setUploadTime(LocalDateTime.now());
            attachment.setUploadBy(uploadBy);
            
            // 保存到数据库
            riskAttachmentMapper.insert(attachment);
            
            return attachment;
        } catch (IOException e) {
            throw new RuntimeException("文件上传失败", e);
        }
    }

    @Override
    public List<RiskAttachment> getAttachmentsByRiskId(Long riskId) {
        return riskAttachmentMapper.selectByRiskId(riskId);
    }

    @Override
    public RiskAttachment getAttachmentById(Long id) {
        return riskAttachmentMapper.selectById(id);
    }

    @Override
    @Transactional
    public boolean deleteAttachment(Long attachmentId) {
        RiskAttachment attachment = riskAttachmentMapper.selectById(attachmentId);
        if (attachment != null) {
            // 删除物理文件
            File file = new File(attachment.getFilePath());
            if (file.exists()) {
                file.delete();
            }
            // 删除数据库记录
            return riskAttachmentMapper.deleteById(attachmentId) > 0;
        }
        return false;
    }
} 